<template>
  <div class="app-container">
    <div v-if="truckDivShow">
      <div style="background-color: white; border-radius: 10px; height: 100px;margin-left: 10px;text-align: center;padding-top: 30px">
        <!-- 多条件分页查询所需条件的form -->
        <el-form :inline="true"  :model="Truck_driver" class="demo-form-inline">
          <el-form-item label="司机名称">
            <el-input ref="inputRef" v-model="Truck_driver.name" placeholder="请输入司机名称"></el-input>
          </el-form-item>
          <el-form-item label="所属车队">
            <el-select v-model="Truck_driver.feel_id" placeholder="请选择状态">
              <el-option label="请选择车队名称" value=""></el-option>
              <el-option v-for="item in fleet" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="query">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="resetValues">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- end -->

      <div style="background-color: white;margin-top: 10px; margin-bottom: 20px;margin-left: 10px;border-radius: 10px;height: 50%">

                <el-button type="primary" @click="adddriver = true" style="margin-left: 30px;margin-top: 30px">+新增司机</el-button>

        <div style="height: 20px"></div>

        <!-- table 用于展示项目建议信息 -->
        <el-table
          v-if="truckShow"
          v-loading="trucklistLoading"
          :data="truck_driverlist"
          style="width: 96%;margin-left: 32px"
          :max-height="500"
          :row-style="{ height: '55px' }"
        >
          <el-table-column fixed label="司机编号" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.driver_id }}
            </template>
          </el-table-column>
          <el-table-column label="司机名称" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.user_name }}
            </template>
          </el-table-column>
          <el-table-column label="司机电话" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.user_phone }}
            </template>
          </el-table-column>
          <el-table-column prop="name" label="所属车队" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.fleet_name }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="DetailsShow(scope.row)">详细信息</el-button>
              <el-button type="text" size="small" @click="opendriver(scope.row)" >编辑车队</el-button>
              <!--.<el-button @click.native.prevent="open(scope.row.id)" type="text" size="small">删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <!-- end --->

        <div style="height: 50px;margin-bottom: 10px">

          <!-- 分页组件 -->
          <el-pagination
            background
            @current-change="change"
            @size-change="pageSizeChange"
            :current-page="truckPageNumber"
            :page-sizes="[10, 20, 40, 80]"
            :total="truckTotal"
            layout="total, prev, pager, next, sizes,jumper"
            style="text-align: center; font-size: 8px; margin-top: 15px"
          >
          </el-pagination>
          <!-- end -->
        </div>
        <div v-if="dialogVisible">
          <el-dialog
            title="修改车队"
            :visible.sync="dialogVisible"
            :before-close="handleClose">
            <el-form :inline="true" :model="updatedirver" class="demo-form-inline">
              <el-form-item label="司机名称">
                <el-input v-model="updatedirver.user_name" placeholder="司机" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="车队">
                <el-select v-model="updatedirver.fleet_id" placeholder="车队">
                  <el-option v-for="item in fleet" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="updates()">修 改</el-button>
            </span>
          </el-dialog>
        </div>

      </div> <!-- 间隔 -->
    </div>
    <div v-if="truckDetailsDivShow">
      <div style="background-color: white; width: 200px; height: 880px; border-radius: 10px; float: left;margin-left: 10px;text-align: center;">
        <el-link :underline="currentTab !== 'basic'" :disabled="currentTab === 'basic'" @click="handleClick('basic')" :style="{ color: currentTab === 'basic' ? '#ff8c00' : 'black' }" class="linkStyle">基本信息</el-link><br>
        <el-link :underline="currentTab !== 'license'" :disabled="currentTab === 'license'" @click="handleClick('license')" :style="{ color: currentTab === 'license' ? '#ff8c00' : 'black' }" class="linkStyle">驾驶证信息</el-link><br>
        <el-link :underline="currentTab !== 'vehicle'" :disabled="currentTab === 'vehicle'" @click="handleClick('vehicle')" :style="{ color: currentTab === 'vehicle' ? '#ff8c00' : 'black' }" class="linkStyle">车辆信息</el-link><br>
      </div>
      <div style="background-color: white; width: 1225px; height: 880px; border-radius: 10px; float: right;">
        <div v-if="baseShow">
          <div class="container">
            <h1 class="title">基本信息</h1>
            <el-button type="danger" class="close-button" @click="goBack()">×</el-button>
          </div>
          <div style="margin-left: 100px;margin-top: 40px">
            <el-form ref="form" :model="this.elemental" label-width="80px">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="员工编号" style="width: 385px">
                    <el-input type="text" v-model="this.elemental.driver_id" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="司机名称" style="width: 385px">
                    <el-input type="text" v-model="this.elemental.user_name" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="所属车队" style="width: 385px">
                    <el-input type="text" v-model="this.elemental.fleet_name" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="电话" style="width: 385px">
                    <el-input type="text" v-model="this.elemental.user_phone" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="年龄" style="width: 385px">
                    <el-input type="text" v-model="this.elemental.age" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
          <hr style="width: 90%; height: 1px; background-color: #cccccc;">
          <div style="text-align: center">
            <el-button type="info" @click="recallButton()" style="margin-left: 30px;margin-top: 30px">返回</el-button>
          </div>
        </div>
        <div v-if="licenseShow">
          <div class="container">
            <h1 class="title">驾驶证信息</h1>
            <el-button type="danger" class="close-button" @click="goBack()">×</el-button>
          </div>
          <div style="margin-left: 100px;margin-top: 40px">
            <el-form ref="form" :model="driverlicense" label-width="80px">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="驾驶证号" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.license_id" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="准驾车型" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.allowable_type" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="初次领证日期" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.initial_certificate_date" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="驾证有效期限" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.valid_period" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="驾龄" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.driver_age" :disabled="true">
                      <template slot="append">年</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="驾驶证类型" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.license_type" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="从业资格证" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.qualification_certificate" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="入场证信息" style="width: 385px">
                    <el-input type="text" v-model="this.driverlicense.pass_certificate" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="驾驶证照片" style="width: 385px">
                    <el-upload
                      class="avatar-uploader"
                      action="http://192.168.16.43:9999/api/pd-base/truckLicense/uploadFile"
                      :limit="1"
                      :on-success="addHandleAvatarSuccess"
                      :auto-upload="false"
                      ref="uploadAdd3">
                      <el-image v-if="this.image.license_picture" :src="this.image.license_picture" class="avatar">
                      </el-image>
                    </el-upload>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
          <hr style="width: 90%; height: 1px; background-color: #cccccc;">
          <div style="text-align: center">
            <el-button type="info" @click="goBack()" style="margin-left: 30px;margin-top: 30px">取消</el-button>
          </div>
        </div>
        <div v-if="jiazhao">
          <div class="container">
            <h1 class="title">驾驶证信息</h1>
            <el-button type="danger" class="close-button" @click="goBack()">×</el-button>
          </div>
          <div>
            <el-button type="primary" @click="adddriver_license_name(elemental)" >+添加驾照信息</el-button>
            <h1>暂无驾照信息</h1>
          </div>
        </div>
        <div v-if="lineTruckNumShow">
          <div class="container">
            <h1 class="title">车辆信息</h1>
            <el-button type="danger" class="close-button" @click="goBack()">×</el-button>
          </div>
          <el-form ref="form" :model="driverjob" label-width="80px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="当前使用车辆" style="width: 385px">
                  <el-input type="text" v-model="this.driverjob.license_plate" :disabled="true"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="车辆所属路线" style="width: 385px">
                  <el-input type="text" v-model="this.driverjob.transport_line_name" :disabled="true"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="车辆所属车次" style="width: 385px">
                  <el-input type="text" v-model="this.driverjob.transport_trips_id" :disabled="true"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <hr style="width: 90%; height: 1px; background-color: #cccccc;">
      </div>
    </div>
    <div>
      <el-dialog
        title="添加司机"
        :visible.sync="adddriver"
        width="30%"
        :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="司机员工">
            <el-select v-model="Truck_driverDto.driver_user_id" placeholder="请选择该司机所属员工">
              <el-option label="请选择员工" value="0"></el-option>
              <el-option v-for="item in this.users"  :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属车队">
            <el-select v-model="Truck_driverDto.fleet_id" placeholder="请选择该司机所属车队">
              <el-option label="请选择车队" value="0"></el-option>
              <el-option v-for="item in this.fleet"  :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="司机年龄">
            <el-input v-model="Truck_driverDto.age">
              <template slot="append">岁</template>
            </el-input>
          </el-form-item>
          <el-form-item label="司机驾龄">
            <el-input v-model="Truck_driverDto.driver_age">
              <template slot="append">年</template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit()">立即创建</el-button>
            <el-button @click="adddriver = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <div>
      <el-dialog
        title="为司机添加驾驶证"
        :visible.sync="adddriver_license"
        width="30%"
        :before-close="handleClose">
        <el-form ref="form" :model="Driver_license" label-width="80px">
          <el-form-item label="司机编号">
            <el-input v-model="Driver_license.license_user_id" disabled="disabled">
            </el-input>
          </el-form-item>
          <el-form-item label="驾驶证号">
            <el-input v-model="Driver_license.license_number" placeholder="请输入驾驶证号">
            </el-input>
          </el-form-item>
          <el-form-item label="准驾车型">
            <el-input v-model="Driver_license.allowable_type" placeholder="请输入准驾车型">
            </el-input>
          </el-form-item>
          <el-form-item label="初领日期">
            <el-date-picker type="date" placeholder="选择日期" v-model="Driver_license.initial_certificate_date" format="yyyy-MM-dd HH:mm:ss"  value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;"></el-date-picker>
          </el-form-item>
          <el-form-item label="有效期">
            <el-date-picker type="date" placeholder="选择日期" v-model="Driver_license.valid_period" format="yyyy-MM-dd HH:mm:ss"  value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;"></el-date-picker>
          </el-form-item>
          <el-form-item label="司机驾龄">
            <el-input v-model="Driver_license.driver_age">
              <template slot="append">年</template>
            </el-input>
          </el-form-item>
          <el-form-item label="驾驶证类型">
            <el-input v-model="Driver_license.license_type">
            </el-input>
          </el-form-item>
          <el-form-item label="驾驶资格证">
            <el-input v-model="Driver_license.qualification_certificate">
            </el-input>
          </el-form-item>
          <el-form-item label="入场资格信息">
            <el-input v-model="Driver_license.pass_certificate">
            </el-input>
          </el-form-item>
          <div>
          <el-form-item label="驾驶证图片" label-width="100px">
            <el-upload
              class="avatar-uploader"
              action="http://192.168.16.43:9999/api/pd-base/truckLicense/uploadFile"
              :limit="1"
              :on-success="onSubmit_license"
              :auto-upload="false"
              ref="uploadAdd">
              <div class="avatar">
              <div slot="error" class="image-slot" style="color: #99a9bf">
                <i class="el-icon-plus avatar-uploader-icon"></i>
              </div>
              </div>
            </el-upload>
          </el-form-item>
          </div>
          <el-form-item>
            <el-button type="primary" @click="onSubmit_license()">添加</el-button>
            <el-button @click="adddriver_license = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>

</template>

<script>

import {
  addTruckDriver, addtruckdriverlicense, getAllFleetByAgency,
  getimagesUrl,
  getTruckDriverLicense,
  queryTruckDriverVo,
  updateTruckDriverVo, userRoleName
} from '@/api/truckdriver/truckdriver'
import { findTruck_driverMobileVo } from '@/api/Driver_Side/Driver_Side'
import Amaps from '@/views/work/AMap.vue'
import user from '@/store/modules/user'

export default {
  components: { Amaps },
  data(){
    return {
      adddriver_license:false,
      addlicense:false,
      Driver_license:{
        license_user_id:'',
        allowable_type:'',
        initial_certificate_date:'',
        valid_period:'',
        driver_age:'',
        license_type:'',
        qualification_certificate:'',
        pass_certificate:'',
        license_picture:''
      },
      adddriver:false,
      image:{
        license_picture:'',
      },
      Truck_driverDto:{},
      driverlicense:{},
      deliver_picture:'',
      transport_certificate:'',
      cargo_pick_up_picture:'',
      cargo_picture:'',
      lineTruckNumShow:false,
      licenseShow:false,
      baseShow:false,
      currentTab: 'basic',
      truckDivShow:true,
      truckDetailsDivShow: false,
      selectedValue: '',
      fleet:{},
      truck_type:[],
      logo: 'add',
      truckShow:true,
      age1:'',
      age2:'',
      title:'添加项目',
      drawer: false,
      Truck_driver:{
        feel_id:'',
        name:'',
      },
      driverjob:{
        transport_trips_id:'暂无',
        license_plate:'暂无',
        transport_line_name:'暂无',
      },
      truck_driverlist: [],
      count:0,
      trucklistLoading: true,
      truckPageNumber: 1,
      truckPageSize: 10,
      truckTotal: 0,
      options:[],
      updatedirver:{
        driver_id:'',
        feel_id:'',
      },
      dialogVisible: false,
      users:[],
      jiazhao:false
    }
  },
  created() {
    this.init()
  },
  methods:{
    query(){
      this.truckPageNumber = 1;
      this.init()

    },
    init(){
      const truck_driverDto = {
      pageNum: this.truckPageNumber,
      pageSize: this.truckPageSize,
      feel_id: this.Truck_driver.feel_id,
      name: this.Truck_driver.name,
      org_id : user.state.orgId
    }
      getAllFleetByAgency(user.state.id).then(response=>{
        this.fleet = response.data
        truck_driverDto.org_id = user.state.orgId
        queryTruckDriverVo(truck_driverDto).then(response=>{
          this.truck_driverlist = response.data.list
          this.trucklistLoading = false
          this.truckTotal = response.data.total
          this.truckShow = true
          const dto = {
            org_id: user.state.orgId,
            role_name: '司机'
          }
          userRoleName(dto).then(response=>{
            this.users = response.data
          })
        })
      })
      this.trucklistLoading = true
    },
    goBack(){
      this.jiazhao = false
      this.licenseShow = false
      this.lineTruckNumShow = false
      this.currentTab = 'basic'
      this.truckDivShow = true
      this.truckDetailsDivShow = false
      this.TruckDetails={
        id:null,
        agency:null,
        fName:null,
        license_plate:null,
        tName:null,
        allowable_load:null,
        allowable_volume:null,
        device_gps_id:null,
        work_status:null,
        load_status:null,
        status:null
      }
    },
    handleClick(tab) {
      this.jiazhao = false
      this.currentTab = tab;
      this.saveButton = false
      if (tab === "basic"){
        this.baseShow = true
        this.licenseShow = false
        this.lineTruckNumShow = false
      }else if (tab === "license"){
        getTruckDriverLicense(this.elemental.driver_user_id).then(response=>{
          if(response.data !== null){
            this.jiazhao = false
            this.licenseShow = true
            this.driverlicense = response.data
            this.getimageUrls(this.driverlicense.license_picture)
          }else{
            this.licenseShow = false
            this.jiazhao = true
          }
        })
        this.baseShow = false
        this.lineTruckNumShow = false
      }else if(tab === "vehicle"){
        this.licenseShow = false
        this.baseShow = false
        this.lineTruckNumShow = true
        this.transport = []
        findTruck_driverMobileVo(this.elemental.driver_user_id).then(response=>{
          if(response.data === null || response.data === ''){
            this.driverjob.transport_trips_id = '暂无'
            this.driverjob.license_plate = '暂无'
            this.driverjob.transport_line_name = '暂无'
          }else{
            this.driverjob = response.data
          }
        })
      }
    },
    getimageUrls(license_picture){
      getimagesUrl(license_picture).then(response=>{
        this.image.license_picture = response.data
      })
    },
    recallButton(){
      this.goBack()
    },
    DetailsShow(data){
      this.elemental = data;
      this.truckDivShow = false
      this.baseShow = true
      this.truckDetailsDivShow = true;
    }
    ,
    resetValues() {
      this.$refs.inputRef.clear(); // 清空输入框的值
      this.truckDto.truck_type_id=''
      this.truckDto.fleet_id='' // 清空下拉列表框的选中项
    }
    ,
    change(val){
      this.truckPageNumber = val;
      this.init();
    },
    pageSizeChange(newPageSize) {
      this.truckPageSize = newPageSize;

      this.init()
    },
    agree(logo){
      if(logo==='add'){
        this.saveEvaluate()
      }else{
        this.modify()
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.form = {
            te_id:0,
            sportName: '',
            gender: null,
            intensity: null,
            equivalent: '',
            age:this.age1+this.age2,
            time: '',
            part: null,
            method: '',
            notes: '',
            status:null
          },
            this.age1=null
          this.age2=null
          done();
        })
        .catch(_ => {});
    },
    //删除车辆信息(其实就是修改车辆状态)
    opendriver(res) {
      this.updatedirver = res
      this.dialogVisible = true;
    },
    updates(){
      this.$confirm('是否继续修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const updatee = {
          driver_id : this.updatedirver.driver_id,
          fleet_id : this.updatedirver.fleet_id
        }
        updateTruckDriverVo(updatee).then(response=>{
          if(response.flag === "true"){
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
            this.dialogVisible = false
            this.init()
          }else{
            this.$message({
              type: 'error',
              message: '修改失败!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    addHandleAvatarSuccess(res) {
      this.driverlicense.driver_picture = res.data
    },
    onSubmit(){
      addTruckDriver(this.Truck_driverDto).then(response=>{
        this.$message({
          type: 'success',
          message: '添加成功!'
        });
        this.adddriver = false
        this.Truck_driverDto = {}
      })
    },
    onSubmit_license(res){
      this.$refs.uploadAdd.submit();
      this.Driver_license.license_picture = res.data
      this.Driver_license.license_user_id =
      addtruckdriverlicense(this.Driver_license).then(response=>{
        this.$message({
          type: 'success',
          message: response.message
        });
        this.adddriver_license = false
        this.Driver_license = {}
      })
    },
    adddriver_license_name(res){
      this.adddriver_license = true
      this.Driver_license.license_user_id = res.driver_user_id
    }
  }
}
</script>

<style scoped lang="scss">
.app-container{
  width: 100%;
  height: 908px;
  background-color: #f1f1f1; /* 设置背景颜色 */
}
.linkStyle{
  font-size: 14px;
  margin: 10px 0;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
.container {
  display: flex;
  align-items: center;
}

.title {
  margin-left: 50px;
  margin-top: 30px;
  margin-right: 20px; /* 调整按钮与标题之间的间距 */
  color: black;
}

.close-button {
  margin-left: 900px; /* 将按钮推到最左边 */
}
.el-form-item .el-form-item__label {
  width: auto; /* 自适应宽度 */
  white-space: nowrap; /* 不换行 */
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

